<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<meta charset="UTF-8">
<title>成绩查询</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<link rel="stylesheet" href="../static/lib/layui/css/layui.css" th:href="@{/lib/layui/css/layui.css}" media="all">
<script src="../static/lib/layui/layui.js" th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
<script src="../static/lib/jquery/jquery-3.2.1.min.js" th:src="@{/lib/jquery/jquery-3.2.1.min.js}"></script>
<script src="../static/js/common/public.js" th:src="@{/js/common/public.js}"></script>
<style type="text/css">
    .form-padding {
        padding: 4px;
    }
    #search-form .layui-input-inline{
        width:120px;
    }
</style>
</head>
<body>
<form class="layui-form form-padding" id="search-form">
    <div class="layui-form-item">
        <label class="layui-form-label">昵称</label>
        <div class="layui-input-inline">
            <input type="text" name="username" autocomplete="on"
                   class="layui-input">
        </div>
        <label class="layui-form-label">账号</label>
        <div class="layui-input-inline">
            <input type="text" name="id" required autocomplete="on"
                   class="layui-input">
        </div>
        <label class="layui-form-label">性别</label>
        <div class="layui-input-inline">
            <select id="alarmType-select" name="sex" lay-verify="required">
                <option value="">全部</option>
                <option value="1">男</option>
                <option value="2">女</option>
            </select>
        </div>
        <div class="layui-input-inline">
            <div class="layui-btn" id="submit">搜索</div>
        </div>
    </div>
</form>
<table id="list" lay-filter="data-table"></table>
<script type="text/html" id="toolBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="eve1">添加好友</a>
</script>

<script type="application/javascript" th:inline="none">
    (function () {
        var layer;
        var layim;
        layui.use('element', function () {
            var element = layui.element;
        });
        layui.use('form', function () {
            /*var form = layui.form;
            //监听提交
            form.on('submit(formDemo)', function (data) {
                layer.msg(JSON.stringify(data.field));
                return false;
            });*/
        });

        layui.use('layer', function () {
            layer = layui.layer;
        });
        layui.use('layim', function () {
            layim = layui.layim;
        });
        function bindAction() {
            $('#submit').click(function () {
                search(constructArgs($('#search-form')))
            })
        }

        function search(param) {
            $.ajax({
                url: '/inside/searchFriend',
                data: param,
                success: function (data) {
                    try {
                        if (data.code == 0) {
                            render(data.data);
                        } else {
                            layer.msg(data.description)
                        }
                    } catch (e) {
                        //location.href='/showLogin'
                    }
                }
            })
        }
        function addFriend(param) {
            $.ajax({
                url: '/inside/addFriend',
                type:'POST',
                data: param,
                success: function (data) {
                    layer.msg(data.msg)
                    console.log(data);
                    var o = data.data;
                    o.groupid = 1;
                    o.type = 'friend';
                    parent.outerLayim.addList(o);
                }
            })
        }
        function render(data) {
            var table = null;
            layui.use('table', function () {
                table = layui.table;
                table.render({
                    elem: '#list'
                    //,width: 900
                    //,height: 274
                    , cellMinWidth: 100
                    , cols: [[
                        {field: 'id', title: '账号', align: 'center'}
                        , {field: 'username', title: '昵称', align: 'center'}
                        , {field: 'sex', title: '性别', align: 'center'}
                        , {field: 'sign', title: '个性签名', align: 'center'}
                        , {fixed: 'right', title: '操作', toolbar: '#toolBar', align: 'center',width:120}

                    ]]
                    , data: data
                    , skin: 'row' //表格风格
                    , even: true
                    //,size: 'lg' //尺寸
                    , page: true //是否显示分页
                    //, limits: [3, 5, 10]
                    , limit: 10 //每页默认显示的数量
                    , loading: false //请求数据时，是否显示loading
                });
                table.on('tool(data-table)', function (obj) {
                    let value = obj.value //得到修改后的值
                        , data = obj.data //得到所在行所有键值
                        , field = obj.field; //得到字段
                    if (obj.event === 'eve1') {
                        layim.add({
                            type: 'friend' //friend：申请加好友、group：申请加群
                            ,username: data.username //好友昵称，若申请加群，参数为：groupname
                            ,avatar: data.avatar //头像
                            ,submit: function(group, remark, index){ //一般在此执行Ajax和WS，以通知对方
                                addFriend({
                                    friendId:data.id
                                })
                               /* console.log(group); //获取选择的好友分组ID，若为添加群，则不返回值
                                console.log(remark); //获取附加信息
                                layer.close(index); //关闭改面板*/
                            }
                        });
                    } else {

                    }
                });
            });
        };
        bindAction();
    })()

</script>
</body>
</html>